<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>重置您的密码</title>
    <style type="text/css">
        body {
            background-image: url('https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_1280.jpg');
            background-size: cover;
            background-position: center;
            font-family: Arial, sans-serif;
        }
        #resetform {
            margin-top: 100px;
            padding: 35px;
            background-color: rgba(255, 255, 255, 0.8);
            border-radius: 10px;
            max-width: 400px;
            margin-left: auto;
            margin-right: auto;
        }
        input {
            width: 100%;
            padding: 10px;
            margin: 15px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        input[type="submit"], input[type="reset"] {
            width: 48%;
            background-color: #4CAF50;
            color: white;
            border: none;
        }
        input[type="submit"]:hover, input[type="reset"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
    <form method="post" id="resetform">
        <input type="text" id="userphone" name="userphone" placeholder="输入您的手机号码">
        <input type="password" id="resetpass1" name="resetpass1" placeholder="输入您的新密码">
        <input type="password" id="resetpass2" name="resetpass2" placeholder="再次输入您的新密码">
        <input type="reset" value="清空">
        <input type="button" id="resetbtn" onclick="resetpass()" value="提交">
    </form>
    <script src="static/js/jquery-3.0.0.js"></script>
    <script type="text/javascript">
        document.getElementById('resetform').onsubmit = function(event) {
            event.preventDefault();
            resetpass();
        };

        function resetpass() {
            var userphone = document.getElementById("userphone").value;
            var resetpass1 = document.getElementById("resetpass1").value;
            var resetpass2 = document.getElementById("resetpass2").value;
            var submit_flag = 1;

            //判空
            if ((userphone.length == 0) || (resetpass1.length == 0) || (resetpass2.length == 0)) {
                submit_flag = 0;
                alert("请把信息填写完整！");
                return;
            }
            //判断密码一致性
            if (resetpass2 != resetpass1) {
                submit_flag = 0;
                alert("两次填写的密码不一致");
                document.getElementById("resetpass1").focus();
                return;
            }
            //判断手机号
            if (userphone.length != 11) {
                submit_flag = 0;
                alert("手机号码应为11位！");
                document.getElementById("userphone").focus();
                return;
            }
            var regu = /^1[3456789]\d{9}$/;
            if (!(regu.test(userphone))) {
                submit_flag = 0;
                alert("手机号码格式有误！");
                document.getElementById("userphone").focus();
                return;
            }
            //发起请求
            if (submit_flag == 1) {
                $.ajax({
                    url: "/resetpass",
                    data: { userphone: userphone, resetpass: resetpass2 },
                    type: "POST",
                    success: function (data) {
                        if (data.data == 1) {
                            alert("密码修改成功！");
                            window.location.href = "/login"
                        } else {
                            alert("修改密码失败！请重试");
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        alert("请求失败！");
                    }
                });
            }
        }
    </script>
</body>
</html>
